var s;

function createData(v) {
	var d = new Object();
	d.id = v;
	d.text = v;
	return d;
}

$(document).ready(function(){
	
	$('#input_title').poshytip({
		className: 'tip-yellow',
		bgImageFrameSize: 18,
		showOn: 'focus',
		alignTo: 'target',
		alignX: 'right',
		alignY: 'center',
		offsetX: 25
	});
	
	$('#input_theme').poshytip({
		className: 'tip-yellow',
		bgImageFrameSize: 18,
		showOn: 'focus',
		alignTo: 'target',
		alignX: 'right',
		alignY: 'center',
		offsetX: 25
	});
	
	$('#input_locate').poshytip({
		className: 'tip-yellow',
		bgImageFrameSize: 18,
		showOn: 'focus',
		alignTo: 'target',
		alignX: 'right',
		alignY: 'center',
		offsetX: 25
	});
	
	$('#input_content').poshytip({
		className: 'tip-yellow',
		bgImageFrameSize: 18,
		showOn: 'focus',
		alignTo: 'target',
		alignX: 'right',
		alignY: 'center',
		offsetX: 25
	});

	$("#contact").select2({
		tags:my_friend,
		tokenSeparators: [","]
	});
	
	/*
	$("#contact").on("change", function() { $("#contact_val").html($("#contact").val());});
	$("#contact").select2("container").find("ul.select2-choices").sortable({
		containment: 'parent',
		start: function() { $("#contact").select2("onSortStart"); },
		update: function() { $("#contact").select2("onSortEnd"); }
	});
	*/
	
	$("#my_contact_btn").click(function() {
		$("#dialog").dialog({
			resizable: false,
			height: 400,
			width: 620,
			modal: true,
			show: "blind",
			hide: "blind",
			buttons: {
				确定:function(){
					$(this).dialog("close");
					//alert("您点击了登录按钮");
					//for(e in temp_name) {
					//	// 这一段留作纪念
					//	//var listring = "<li class='select2-search-choice'>" +
					//	//			   "    <div>" + temp_name[e] + "</div>" +
					//	//			   "    <a href='#' onclick='return false;' class='select2-search-choice-close' tabindex='-1'></a>" +
					//	//			   "</li>"
					//	//listring.replace("<div></div>","<div>"+temp_name+"</div>");
					//	//console.log(listring);
					//	//$("#tagul")[0].innerHTML=$("#tagul")[0].innerHTML + listring;
					//	ii = find(temp_phone[e], all_phone);
					//	if (ii == -1) {
					//		all_name.push(temp_name[e]);
					//		all_phone.push(temp_phone[e]);
					//		var d = createData(temp_name[e] + " " + temp_phone[e]);
					//		s.addSelectedChoice(d); // （不安全待改进）全局变量 s 在 MultiSelect2 initContainer 的时候获取
					//	}
					//}
				}
			}
		});
	});
	
	$("#input_locate").focus(function(){
		$("#input_locate").animate({height:"150px"});
	});
	
	$("#input_locate").blur(function(){
		$("#input_locate").animate({height:"40px"});
	});
	
	$("#input_content").focus(function(){
		$("#input_content").animate({height:"150px"});
	});
	
	$("#input_content").blur(function(){
		$("#input_content").animate({height:"40px"});
	});
	
	$(".tagspan").click(function(){
		var selectedDIV = $(this);
		var selectedName = selectedDIV.find("h5")[0].innerHTML;
		var selectedPhone = selectedDIV.find("span")[0].innerHTML;
		//alert(selectedName+" "+selectedPhone);
		//console.log("name:" + selectedName + "\nphone:" + selectedPhone + "\n");
		//alert(selectedDIV[0].innerHTML);
		if(selectedDIV.hasClass("b1")) {
			selectedDIV.removeClass("b1");
			selectedDIV.addClass("b2");
			//temp_name.push(selectedName);
			//temp_phone.push(selectedPhone);
			//console.log("temp_name length: " + temp_name.length + "\ntemp_phone length: " + temp_phone.length + "\n");
			//all_name.push(selectedName);
			//all_phone.push(selectedPhone);
			var d = createData(selectedName + " " + selectedPhone);
			s.addSelectedChoice(d); // （不安全待改进）全局变量 s 在 MultiSelect2 initContainer 的时候获取
		}
		else if (selectedDIV.hasClass("b2")) {
			selectedDIV.removeClass("b2");
			selectedDIV.addClass("b1");
			i = find(selectedPhone, all_phone);
			if (i != -1) { // 如果 unselect 的联系人在数组中
				arr = $("#tagul").find("li");
				var e;
				for (e in arr) { // 遍历 tag 找到 tag 的下标
					if($(arr[e]).find("span").html() == selectedPhone) {
						break;
					}
				}
				$(arr[e]).find(".select2-search-choice-close").click(); // 删除 tag
			}
			//i = find(selectedPhone, temp_phone);
			//console.log("index: " + i);
			//temp_name.splice(i, 1);
			//temp_phone.splice(i, 1);
			//console.log("temp_name length: " + temp_name.length + "\ntemp_phone length: " + temp_phone.length + "\n");
		}
	});
	
	$('#contact').attr("title","键入姓名、空格、手机号，然后按回车键完成一个联系人的输入，或者您也可以从我的联系人当中直接选择");
	
	/*
	$('#contact').poshytip({
		className: 'tip-yellow',
		bgImageFrameSize: 18,
		showOn: 'focus',
		alignTo: 'target',
		alignX: 'right',
		alignY: 'center',
		offsetX: 25
	});
	
	$('.select2-input').focus(function() {
		('#contact').poshytip('show');
	});
	
	$('.select2-input').blur(function() {
		('#contact').poshytip('hide');
	});
	*/
});